//
//  CommendCollectionCell.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/2/9.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "CommendCollectionCell.h"
#import "NewContactCell.h"
#import "ClubTextImageCell.h"
#import <SDWebImageManager.h>
#import "ImageTitleModel.h"
#import "LottieLoading.h"

@interface CommendCollectionCell()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;

@end

@implementation CommendCollectionCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
    }
    return self;
}

- (void)setModel:(CommendModel *)model
{
    _model = model;
    self.tableView.contentOffset = CGPointMake(0, 0);
    [self.contentView addSubview:self.tableView];
    //赋值完就去下载去
    [self calculateImagesHeight:model.atlasList];
}

#pragma mark - Table view data delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 250;
    }
    CommendImageTextModel *textModel = self.model.atlasList[indexPath.row-1];
    if (textModel.cellH == 0 || !textModel.isFinshDownLoad || !textModel.isCalculateAga) {//没有下载完就来计算的话 model里面有高度值 再进来就不计算了,就算标记下载完了 还没有去重新算一遍
        ClubTextImageCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ClubTextImageCell class])];
        textModel.cellH = [cell calculateComendCellHWithModel:textModel];
        if (textModel.isFinshDownLoad) {
            textModel.isCalculateAga = YES;
        }
    }
    return textModel.cellH;
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1 + self.model.atlasList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        NewContactCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([NewContactCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.model;
        cell.zanClickBlock = ^(CommendModel *model, UIButton *btn) {
            if (self.zanClickBlock) {
                self.zanClickBlock(model, btn);
            }
        };
        cell.shareBlock = ^(CommendModel *model) {
            if (self.shareBlock) {
                self.shareBlock(model);
            }
        };
        return cell;
    }
    ClubTextImageCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ClubTextImageCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.commendModel = self.model.atlasList[indexPath.row-1];
    cell.shadowOffset = 2.0;
    return cell;
}
//缓存图片计算图片高度
- (void)calculateImagesHeight:(NSArray *)arr
{
    __WEAK
//    [LottieLoading showLoadingInView:self];
    dispatch_group_t group = dispatch_group_create();
    for (CommendImageTextModel *model in arr) {
        dispatch_group_enter(group);
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:model.image] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            if (finished) {
                model.isFinshDownLoad = YES;
                [self.tableView reloadData];//下载完就计算一次
                dispatch_group_leave(group);
            }
        }];
    }
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//        [LottieLoading hideLoadingInView:self];//下载完图片在隐藏指示器
//        [weakSelf.tableView reloadData];
    });
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW,ScreenH-SafeAreaTopHeight-SafeAreaBottomHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.sectionFooterHeight = 0.01;
        _tableView.backgroundColor = RGBColor(239, 239, 244);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView = [[UIView alloc]init];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([NewContactCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([NewContactCell class])];
        [_tableView registerClass:[ClubTextImageCell class] forCellReuseIdentifier:NSStringFromClass([ClubTextImageCell class])];
    }
    return _tableView;
}

@end
